//Andreas S. Baum
//CS 14/ES 20 HOMEWORK 7, Problem 1
//PizzaOrderPanel.java
//05/29/04
//Section Leader: Scott Wisniewski

//This class creates a pizza order form

package ChatClientApplet;


import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

public class ChatPanel extends JPanel implements Runnable
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public final static int LOGIN = 1;
	public final static int CHAT = 2;

	private static Socket socket;
	private LoginPanel login;
	private ChatClientPanel chat;
	private String username;
	boolean isComplete;
	int hostOK;

	Boolean usernameOK = false;

	private String errorMessage;

	private PrintWriter out = null;
	private BufferedReader in = null;

	String host = ChatClientApplet.HOST;
	int port = ChatClientApplet.SERVER_PORT;


	// default constructor:
	public ChatPanel()
	{
		super();
		setBackground(Color.white);
		socket = null;
		isComplete = false;

		hostOK = 1;
		// a panel to log in:
		login = new LoginPanel();
		add(login, BorderLayout.CENTER);

		Thread thread = new Thread(this);
		thread.start();
	}

	public void run() {	
		try {
			// send connection request:
			socket = new Socket(host, port);
		} catch (UnknownHostException e1) {
			// If host can't be found:
			login.setConnected("ERROR: Don't know about host: " + host + ".", Color.red);
			hostOK = 0;
		} catch (IOException e1) {
			// If host doesn't accept connections:
			login.setConnected("ERROR: Couldn't get I/O for "
					+ "the connection to: " + host + ".", Color.red);
			hostOK = 0;
		}

		if (hostOK == 1) {
			login.setConnected("Connected to " + host + ".", Color.blue);
			login.enableInput(true);
		}

		usernameOK = false;
		while (!usernameOK) {
			while (login.getDone() == false) {};
			username = login.getUserName();
			testUsername(username);
			if(!usernameOK) {
				login.setError(errorMessage);
			}
		}

		setSize(ChatClientApplet.WIDTH,ChatClientApplet.HEIGHT);
		setLocation(0,0);
//		a panel for the actual chat:
		chat = new ChatClientPanel(socket, username);


		invalidate();
		remove(login);
		add(chat, BorderLayout.CENTER);
		validate();
		repaint();
	}

	private void testUsername(String testName) {

		try {
			// establish in and out streams for communication with server
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}

		out.println("USR " + testName);
		//		Receive handshake from server (either accepted username
		// or invalid username)
		String response;
		try {
			response = in.readLine();	

			// username is ok
			if (response.toUpperCase().startsWith("PRINT")) {
				usernameOK = true;
			}
			// username is already used
			else {
				errorMessage = response.substring(4);
				usernameOK = false;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}